import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.xml.bind.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;


public class JDBCUtil {


	   public static Document toDocument(ResultSet rs)
	   throws ParserConfigurationException, SQLException
	{
	   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	   DocumentBuilder builder        = factory.newDocumentBuilder();
	   Document doc                   = builder.newDocument();

	   Element results = (Element) doc.createElement("Results");
	   doc.appendChild((Node) results);

	   ResultSetMetaData rsmd = rs.getMetaData();
	   int colCount           = rsmd.getColumnCount();

	   while (rs.next())
	   {
	      Element row = (Element) doc.createElement("Row");
	      ((Node) results).appendChild((Node) row);

	      for (int i = 1; i <= colCount; i++)
	      {
	         String columnName = rsmd.getColumnName(i);
	         Object value      = rs.getObject(i);

	         Element node      = (Element) doc.createElement(columnName);
	         ((Node) node).appendChild(doc.createTextNode(value.toString()));
	         ((Node) row).appendChild((Node) node);
	      }
	   }
	   return doc;
	}

	public static char[] serialize(Document doc) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
